package ynu;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Show {
    private String id;
    private String movieName;
    private String theaterName;
    private Date showTime;
    private double price;
    private int rows;
    private int cols;
    private char[][] seatMap;

    public void setId(String id) {
        this.id = id;
    }

    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }

    public void setTheaterName(String theaterName) {
        this.theaterName = theaterName;
    }

    public void setShowTime(Date showTime) {
        this.showTime = showTime;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }

    public Show(String id, String movieName, String theaterName,
                Date showTime, double price, Theater theater) {
        this.id = id;
        this.movieName = movieName;
        this.theaterName = theaterName;
        this.showTime = showTime;
        this.price = price;
        this.rows = theater.getRowCount();
        this.cols = theater.getSeatCountPerRow();
        this.seatMap = theater.initSeatMap();
    }

    // 带座位状态的构造方法
    public Show(String id, String movieName, String theaterName,
                Date showTime, double price, String seatStatus, Theater theater) {
        this(id, movieName, theaterName, showTime, price, theater);
        if (seatStatus != null && !seatStatus.isEmpty()) {
            this.seatMap = Theater.deserializeSeatMap(seatStatus, rows, cols);
        }
    }

    // 读取所有场次信息
    public static List<Show> readShows() {
        List<Show> shows = new ArrayList<>();
        File file = new File("D:\\java实训大作业\\场次信息.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<Theater> theaters = Theater.readTheaters();

        if (!file.exists()) return shows;

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\|");
                if (parts.length == 6) {
                    try {
                        String theaterName = parts[2];
                        Theater theater = theaters.stream()
                                .filter(t -> t.getName().equals(theaterName))
                                .findFirst()
                                .orElse(null);

                        if (theater != null) {
                            shows.add(new Show(
                                    parts[0], // ID
                                    parts[1], // 影片名
                                    theaterName, // 放映厅
                                    sdf.parse(parts[3]), // 时间
                                    Double.parseDouble(parts[4]), // 票价
                                    parts[5], // 座位状态
                                    theater
                            ));
                        }
                    } catch (ParseException e) {
                        System.out.println("解析日期出错: " + e.getMessage());
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("读取场次信息出错: " + e.getMessage());
        }
        return shows;
    }

    // 保存所有场次信息
    public static void saveShows(List<Show> shows) {
        File dir = new File("D:\\java实训大作业");
        if (!dir.exists()) dir.mkdirs();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                "D:\\java实训大作业\\场次信息.txt"))) {
            for (Show show : shows) {
                bw.write(String.format("%s|%s|%s|%s|%.2f|%s",
                        show.id,
                        show.movieName,
                        show.theaterName,
                        sdf.format(show.showTime),
                        show.price,
                        Theater.serializeSeatMap(show.seatMap))); // 序列化座位图
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("保存场次信息出错: " + e.getMessage());
        }
    }

    // 检查指定场次是否有售出的票
    public boolean hasTicketsSold() {
        for (char[] row : seatMap) {
            for (char seat : row) {
                if (seat == 'X') return true;
            }
        }
        return false;
    }

    // Getters
    public String getId() {
        return id;
    }

    public String getMovieName() {
        return movieName;
    }

    public String getTheaterName() {
        return theaterName;
    }

    public Date getShowTime() {
        return showTime;
    }

    public double getPrice() {
        return price;
    }

    public char[][] getSeatMap() {
        return seatMap;
    }

    public int getRows() {
        return rows;
    }

    public int getCols() {
        return cols;
    }

    // Setters
    public void setSeatMap(char[][] seatMap) {
        this.seatMap = seatMap;
    }
}